$(function () {
	checkImage();
	checkLogin(function () {
		//判断是否是管理员
		$.ajax({
			url: baseurl + '/user/adminInfo.do',
			dataType: 'jsonp',
			success: function (data) {
				if (data.resultCode == 0) {
					if (data.result.admin == 'False' || data.result.admin == 'false' || data.result.admin == false) {
						$('.choose-district').hide();
					}
				}
			}
		})
	});
	getDefaultPoi();
	chooseDistrict();
	//确认发布按钮
	$('.admin-post').click(function () {
		if (!$(this).hasClass('disabled')) {
			searilize();
		}
	})
	//上传图片配置
	if (window.inApp && inApp.android) {
		androidUpload(function (data,target) {
			var ele = $('<li class="uploadfuck"><img></li>');
			ele.find('img').attr('src', data).show();
			$('.sendimg').find('ul').append(ele);
			if ($('.uploadfuck').length == 5) {
				$('.sendimg').find('a').hide();
			}
			var imgs = $('.uploadfuck').find('img').map(function (v, i) { return i.src }).toArray().toString();
			localStorage.imgUpload = imgs;
		});
	}
	else {
		var domain_name = (env == 'pre' || env == 'sit') ? 'cnsuning.com' : 'suning.com';
		document.domain = window.domain = domain_name;
		$('#filechooser').AjaxFileUpload({
			action: baseurl + '/post/save/fileUpload.do',
			onComplete: uploadCallback
		})
	}


	$('.post-post textarea').on('input propertychange', function () {
		var apDisabled = false;
		if ($.trim($(this).val()) == '') {
			$('.admin-post').addClass('disabled')
		} else (
			$('.admin-post').removeClass('disabled')
		)
	})
	$('.post-post .choose-district').click(function () {
		location.href = '../choose-district.html';
	});
})
//获取默认小区
var getDefaultPoi = function () {
	Prepare({
		complete: function (location) {
			console.log(location);
			var poiId = location.poiId;
			var poiName = location.poiName;
			$('.choose-district span').html('（' + poiName + '）');
			$('.choose-district span').attr('poiId', poiId);
		}
	})
}
//检测选择小区
var chooseDistrict = function () {
	var district = localStorage.district;
	if (district) {
		$('.choose-district span').html('（' + district + '）');
		$('.choose-district span').attr('poiId', localStorage.poiId);
	}
}
//上传图片函数
var checkImage = function () {
	if (localStorage.imgUpload) {
		var imgArry = localStorage.imgUpload.split(',');
		for (var i = 0, l = imgArry.length; i < l; i++) {
			var ele = $('<li class="uploadfuck"><img></li>');
			ele.find('img').attr('src', imgArry[i]).show();
			$('.sendimg').find('ul').append(ele);
		}
		if ($('.uploadfuck').length == 5) {
			$('.sendimg').find('a').hide();
		}
	}
	var all = true
	var imgs = $('img');
	for (var i = 0; i < imgs.length; i++) {
		if (!$(imgs[i]).attr('src')) {
			$(imgs[i]).hide();
			all = false;
		}
	}
	if (all) {
		$('.upload-text').hide();
	}
	else {
		$('.upload-text').show();
	}

}
function uploadCallback(data) {
	if (data.result == 0) {
		var ele = $('<li class="uploadfuck"><img></li>');
	    ele.find('img').attr('src', data.url).show();
	    $('.sendimg').find('ul').append(ele);
	    if ($('.uploadfuck').length == 5) {
	        $('.sendimg').find('a').hide();
	    }
	    var imgs = $('.uploadfuck').find('img').map(function(v,i){return i.src}).toArray().toString();
	    localStorage.setItem('imgUpload',imgs);
	}else{
        console.log(data.errorMsg);
		AlertBox({
			msg: data.errorMsg,
			type: 'mini'
		})
	}
}
var searilize = function () {
	checkLogin(function () {
		var postTitle = $('.admin-textarea').val();
		var poiId = $('.choose-district span').attr('poiId');
		var images = localStorage.getItem("imgUpload")?localStorage.getItem("imgUpload"):'';
		console.log(postTitle);
		console.log(images);
		$.ajax({
			url: baseurl + '/post/publish/common.do',
			type: 'GET',
			dataType: 'jsonp',
			data: {
				postTitle: postTitle,
				poiId: poiId,
				images: images
			}
		}).then(function (data) {
			if (data.resultCode == 0) {
				console.log('发布成功');
				AlertBox({
					msg: '发布成功',
					type: 'mini',
				});
				localStorage.removeItem('imgUpload');
				localStorage.removeItem('district');
				localStorage.removeItem('poiId');
				location.href = "/duimen/index.html"
			} else {
				console.log(data.resultMsg);
				AlertBox({
					msg: data.resultMsg,
					type: 'mini'
				})
			}
		})
	})
}